#include "NetworkManager.h"

#include "Log.h"


void NetworkManager::reactiveNetwork(INetwork*const pcurrent, const OfflineReason offlineReason) {
    LOG_WARN("in reactiveNetwork,offline reason:%d", offlineReason);

    if (OfflineReason::SERVER_FAILURE <= offlineReason) {
        return;
    }

    std::unique_lock<MutexType> lock(mmutex);

    if (pcurrent != mpactiveNetwork) {
        mretryCount = 0;

        return;
    }

    if (STOP_START_SHRESHOLD > mretryCount && OfflineReason::HARDWARE_FAILURE == offlineReason) {
        mretryCount = STOP_START_SHRESHOLD;
    } else {
        ++mretryCount;
    }

    if (STOP_START_SHRESHOLD > mretryCount) {
        mpactiveNetwork->active();
    } else {
        if (NetworkType::AUTO == mtype) {
            setActiveNetworkInternal(getNextNetwork());
            mretryCount = 0;

        } else  if (STOP_START_HARDWARE_SHRESHOLD > mretryCount) {
            mpactiveNetwork->stop();
            mpactiveNetwork->start();
        } else if (RESET_SHRESHOLD > mretryCount) {
            mpactiveNetwork->stopHardware();
            mpactiveNetwork->startHardware();
        } else {
            mretryCount = 0;

            LOG_FATAL("all method can not connect to server!");
        }
    }

    lock.unlock();
}



